import * as React from 'react';
import { Redirect, Route } from 'react-router-dom';
import { connect } from 'react-redux';
import { Store, Session } from '../types';
interface Props {
  user: Session['user'],
  path: string,
  component: any
}
class PrivateRoute extends React.Component<Props>{
  render() {
    let {user, path, component: Component} = this.props;
    return (
      <Route path={path} exact render={
        (props) => user.id ? <Component {...props} />:<Redirect to='/login' />
      }/>
    )
  }
}
export default connect((state: Store): Session => {
  return state.Session
})(PrivateRoute)
